package com.aaa.mrcai.filter;

import com.aaa.mrcai.proconst.SystemConst;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author meowyf
 * @version 1.0
 * @ClassName Z_MJ
 * @date 2021/12/21 09 ：14:44
 */
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;

        String contextPath = req.getServletContext().getContextPath();
        String uri = req.getRequestURI();
               uri = uri.replace(contextPath,"");

        String[] passPath = {"/","/adminlogin.html","/userLogin.html"};
        List<String> strings = Arrays.asList(passPath);
        if (strings.contains(uri)){
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        Object attribute = req.getSession().getAttribute(SystemConst.CURRENT_ADMIN_KEY_IN_SESSION);
        if (attribute == null){
            resp.sendRedirect(contextPath);
            return;
        }
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
